var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');



var app = express();


//在所有的路由之前配置cors
const cors = require("cors")
app.use(cors())


app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

//开放静态资源目录
app.use('/uploads', express.static("./uploads"))


//配置自己的路由
//首页
const index = require("./routes/index")
app.use("/index", index)

//同步课程
const course = require("./routes/course")
app.use("/course", course)
//搜索
const search = require("./routes/search")
app.use("/search", search)

//详情
const detail = require("./routes/detail")
app.use("/detail", detail)

// ============================================================

//这是需要登录之后才能访问的接口
const personal = require("./routes/personal")
app.use("/personal", personal)


// catch 404 and forward to error handler
app.use(function (req, res, next) {
  res.send({
    success: false,
    status: 404,
    msg: "该请求不存在"
  })
});

// error handler
app.use(function (err, req, res, next) {
  res.send({
    success: false,
    status: 500,
    msg: err
  })
});

module.exports = app;
